package Z.dailyExercise.April;

import java.util.ArrayList;
import java.util.List;

public class _28实现strStr {
    public static void main(String[] args) {
        _28实现strStr test = new _28实现strStr();
        test.strStr("hello","ll");
    }
    /**
     * 把遍历hay 如果跟needle第一个元素匹配 保存他的下表
     * @param haystack
     * @param needle
     * @return
     */

    public int strStr(String haystack, String needle) {
        if (needle.equals("")  ){
            return 0;
        }
        if (haystack.equals("")){
            return -1;
        }
        List<Integer> list = new ArrayList<>();
        for (int i =0;i<haystack.length();i++){
            if (haystack.charAt(i) == needle.charAt(0)){
                list.add(i);
            }
        }

        int len = needle.length();
        for (int start :list){
            if (start+len > haystack.length()){
                return -1;
            }
            if (haystack.substring(start,start+len).equals(needle)){
                return start;
            }
        }
        return -1;

    }
}
